package com.yxx.service.LogService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxx.entity.VisitLog;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.query.LogQuery;
import com.yxx.mapper.VisitLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 访问日志服务
 * @author yuxiangxun
 * @date 2025/2/15
 * @apiNote
 */
@Service("visitLogService")
public class VisitLogService extends ServiceImpl<VisitLogMapper, VisitLog> {

    @Resource
    private VisitLogMapper visitLogMapper;


    /**
     * 保存访问日志
     * @param visitLog
     */
    public void saveVisitLog(VisitLog visitLog) {
        visitLogMapper.insert(visitLog);
    }

    /**
     * 查询访问日志
     * @param logQuery
     * @return
     */
    public PageResult<VisitLog> listVisitLog(LogQuery logQuery) {
        // 查询访问日志数量
        Long count = visitLogMapper.selectCount(new LambdaQueryWrapper<VisitLog>()
                .like(StringUtils.hasText(logQuery.getKeyword()), VisitLog::getPage, logQuery.getKeyword())
        );
        if (count == 0) {
            return new PageResult<>();
        }
        // 查询访问日志列表
        List<VisitLog> visitLogVOList = visitLogMapper.selectVisitLogList(logQuery);
        return new PageResult<>(visitLogVOList, count);
    }
}
